package shayu.member.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@Configuration
public class ShayuFeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 1.RequestInterceptor 拿到刚进来的请求
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (attributes != null) {
                    log.info("RequestInterceptor" + Thread.currentThread().getId());
                    HttpServletRequest request = attributes.getRequest();
                    if (request != null) {
                        // 同步请求头的数据 Cookie
                        String cookies = request.getHeader("Cookie");
                        template.header("Cookie", cookies);
                    }
                }
            }
        };
    }
}
